﻿using System.Collections.Generic;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ProjectManager.Masters.ModuleData;

namespace ProjectManager.Masters.Forms
{
    public partial class Descriptions : ApplicationLibrary.DBManager.DocumentForm
    {
        private DescriptionType descType;
        private BindableOject<int> descrType = null;

        public Descriptions()
        {
            InitializeComponent();
        }

        public Descriptions(DescriptionType type)
            : base("Descriptions")
        {
            InitializeComponent();
            keyControl = txtCode;
            descType = type;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarDescriptionsParam(descType, txtCode.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerProducts("titleForm", new RadarDescriptions(descType), this);
            dbManager.AddMaster(this, PM_Descriptions.Name, true);

            descrType = new BindableOject<int>();
        }

        protected override void OnBindData()
        {
            BindObject(descrType, PM_Descriptions.Type);
            BindControl(txtCode, PM_Descriptions.Code);
            BindControl(txtDescription, PM_Descriptions.Description);
        }

        protected override bool OnAfterAddNew()
        {
            descrType.Value = (int)descType;
            return base.OnAfterAddNew();
        }

        protected override void OnDisableControlsForEdit()
        {
            txtCode.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        protected override void FocusOnNew()
        {
            txtCode.Focus();
        }

        #endregion Virtual Method
    }

    #region DbManager dbmanagerDescriptions

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerProducts : DBManager
    {
        public dbmanagerProducts(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PM_Descriptions();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PM_Descriptions.Type, "@p1");
            qB.AddCompare(PM_Descriptions.Code, "@p2");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", PM_Descriptions.Type);
            sParam1.Value = PM_Descriptions.Type.DefaultValue;
            PList.Add(sParam1);

            SqlABParameter sParam2 = new SqlABParameter("@p2", PM_Descriptions.Code);
            sParam2.Value = PM_Descriptions.Code.DefaultValue;
            PList.Add(sParam2);
            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
            collection.Parameter[1].Value = key[1];
        }
    }

    #endregion DbManager dbmanagerDescriptions

    public class Products : Descriptions
    {
        public Products()
            : base(DescriptionType.E_Products)
        {
            this.Text = Properties.Resources.F_Products;
        }
    }

    public class Versions : Descriptions
    {
        public Versions()
            : base(DescriptionType.E_Versions)
        {
            this.Text = Properties.Resources.F_Versions;
        }
    }
}